//
//  GZNewFeatureViewCell.swift
//  sinaweibo
//
//  Created by mac book pro on 16/3/23.
//  Copyright © 2016年 finish. All rights reserved.
//

import UIKit

class GZNewFeatureViewCell: UICollectionViewCell {
    var index = 0{
        didSet{
            startButton.hidden = true
            backgroundImageView.image = UIImage(named: "new_feature_\(index)")
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func startButtonAnimation(){
        startButton.hidden = false
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(2, delay: 0.05, usingSpringWithDamping: 0.6, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
                self.startButton.transform = CGAffineTransformMakeScale(1.0, 1.0)
            }) { (_) -> Void in
                print("动画结束")
                AppDelegate.switchRootViewController(GZMainViewController())
        }
    
    }
    private func prepareUI(){
        contentView.addSubview(backgroundImageView)
        contentView.addSubview(startButton)
        //约束
        backgroundImageView.translatesAutoresizingMaskIntoConstraints = false
        startButton.translatesAutoresizingMaskIntoConstraints = false
        contentView.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        //button
        contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -100))

    }
    
    //MARK: -lazy load
    private lazy var backgroundImageView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    private lazy var startButton: UIButton = {
        let startButton = UIButton()
        startButton.setTitle("开始体验", forState: UIControlState.Normal)
        startButton.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
         startButton.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Selected)
        return startButton
    }()
    
}
